package com.example.pjy.service.impl;

import com.example.pjy.mapper.RulesMapper;
import com.example.pjy.mapper.Studentin2Mapper;
import com.example.pjy.mapper.TotalScoreMapper;
import com.example.pjy.pojo.Rules;
import com.example.pjy.pojo.Student;
import com.example.pjy.pojo.Studentin2;
import com.example.pjy.pojo.TotalScore;
import com.example.pjy.service.RulesService;
import com.example.pjy.service.Studentin2Service;
import com.example.pjy.service.Studentin3Service;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Studentin2ServiceImpl implements Studentin2Service {
    @Resource
    private Studentin2Mapper studentin2Mapper;

    @Resource
    private TotalScoreMapper totalScoreMapper;

    @Resource
    private RulesMapper rulesMapper;


    @Override
    public List<Studentin2> list() {
        return studentin2Mapper.list();
    }

    @Override
    public void updatest2(Studentin2 studentin2) {
        studentin2Mapper.updatest2(studentin2);
    }

    @Override
    public void calTotalScore(Integer year) {
        List<Studentin2> list = studentin2Mapper.list();//获取研二学生
        Rules rules = rulesMapper.findByYear(year);//获取年份规则
        System.out.println("获取到的规则：" + rules);
        for (Studentin2 st2 : list) {
            double total = st2.getAcademicScore() * rules.getAcademicScoreWeight() +
                    st2.getPoliticalScore() * rules.getPoliticalScoreWeight() +
                    st2.getResearchScore() * rules.getResearchScoreWeight() +
                    st2.getSocialScore() * rules.getSocialScoreWeight();
            st2.setCalScore(total);//设置这个学生的计算成绩
            System.out.println("学生 " + st2.getStudentId() + " 的计算后成绩：" + total);
            studentin2Mapper.updatest2(st2);//更改这个学生
        }
        //插入学生成绩结束后
    }


    @Override
    public void calAward(Integer year) {
        List<Studentin2> list = studentin2Mapper.list();
        Rules rules = rulesMapper.findByYear(year);
        int awardPeople = 0;
        for (Studentin2 st2 : list) {
            if (st2.getIsqualified() == 1 && awardPeople < rules.getAwardPeople()) {
                awardPeople++;
                st2.setAward(1);
                studentin2Mapper.updatest2(st2);
            }
            else{
                st2.setAward(0);
                studentin2Mapper.updatest2(st2);
            }
        }
    }



    @Override
    public List<Studentin2> selectbyname(String keyword){
        return studentin2Mapper.selectbyname(keyword);
    }
}
